/**
 * 详情对话框
 */
var PoodInfoDlg = {
    data: {
        farmId: "",
        title: "",
        remark: "",
        type: "",
        ph: "",
        temperature: "",
        salt: "",
        createTime: "",
        updateTime: ""
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    var resultData;
    var selectFarmId;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    if (!Feng.isUndefined(Feng.getUrlParam("poodId"))) {
        var ajax = new $ax(Feng.ctxPath + "/api/pood/detail?poodId=" + Feng.getUrlParam("poodId"));
        var result = ajax.start();
        form.val('poodForm', result.data);
        resultData = result.data;
        selectFarmId = resultData.farmId;
    }

    //加载养殖场
    loadFarmList();
    function loadFarmList() {

        $("#farmSelect").html('<option value="">全部养殖场</option>');
        var ajax = new $ax(Feng.ctxPath + "/api/farm/list", function (data) {
            for (var i = 0; i < data.data.length; i++) {
                var name = data.data[i].title;
                var value = data.data[i].farmId;
                var selected = '';
                if (value === selectFarmId) {
                    selected = 'selected';
                }
                if (i === 0 && Feng.isUndefined(selectFarmId) && value !== selectFarmId) {
                    selectFarmId = value;
                    selected = 'selected';
                    $("farmId").val(selectFarmId);
                }
                $("#farmSelect").append('<option value="' + value + '"  ' + selected + '>' + name + '</option>');
            }
            form.render();

        });
        ajax.start();
    }

    form.on('select(farmSelect)', function (data) {
        selectFarmId = data.value;
    });

    //表单提交事件
    var url = "/api/pood/addItem";
    if (!Feng.isUndefined(Feng.getUrlParam("poodId"))) {
        url = "/api/pood/editItem";
    }
    form.on('submit(btnSubmit)', function (data) {
        data.field.farmId = selectFarmId;
        var ajax = new $ax(Feng.ctxPath + url, function (data) {
            Feng.success("操作成功！");
            window.location.href = Feng.ctxPath + '/api/pood'
        }, function (data) {
            Feng.error("操作失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/api/pood'
    });


});